#include "panel.h"
#include <stdio.h>
#include <string.h>
#include "tm1640.h"
#include "led.h"
#include "systimer.h"
#include "controller.h"

const uint8_t tubeNum[10] = {0x77, 0x24, 0x5D, 0x6D, 0x2E, 0x6B, 0x7B, 0x25, 0x7F, 0x6F}; //0 ~ 9

const uint8_t tubeHex[16] = {0x77, 0x24, 0x5D, 0x6D, 0x2E, 0x6B, 0x7B, 0x25, 0x7F, 0x6F, 0x3F, 0x7A, 0x53, 0x7C, 0x5B, 0x1B}; //A ~ F

const uint8_t tubeAlpha[] = {0x3F, 0x7A, 0x53, 0x7C, 0x5B, 0x1B, 0x73, 0x3F, 0x24, 0x74, 0x5E, 0x52, 0x39, 0x38, 0x77};//A ~ G

const uint8_t tubeNegative = 0x08;    // '-'

const uint8_t panelLuminanceTab[] = {
	TM1640_CMD_LIGHT_OFF,
	TM1640_CMD_LIGHT_1_16,
	TM1640_CMD_LIGHT_1_16,
	TM1640_CMD_LIGHT_2_16,
	TM1640_CMD_LIGHT_4_16,
	TM1640_CMD_LIGHT_10_16,
	TM1640_CMD_LIGHT_12_16,
	TM1640_CMD_LIGHT_13_16,
	TM1640_CMD_LIGHT_14_16
};

const Panel panelDefault = 
{
    .code = {0},
    .tube = {
        {{0, 1}, 0, TUBE_TYPE_DEC, 0, 0, 0, 0, 0},
        {{2, 3}, 0, TUBE_TYPE_DEC, 0, 0, 0, 0, 0},
        {{4, 5}, 0, TUBE_TYPE_DEC, 0, 0, 0, 0, 0},
    },
    .unit = UNIT_C,
    .icon = {
        {ICON_STATE_WIFI, 	0, 6, (1 << 3)},	//D64
        {ICON_STATE_LOCK, 	0, 6, (1 << 4)},	//D65
        {ICON_STATE_AC, 	0, 6, (1 << 5)},	//D66
        {ICON_STATE_DN, 	0, 6, (1 << 6)},	//D67
        {ICON_MODE_FRESH, 	0, 6, (1 << 7)},	//D68
        {ICON_MODE_ECO, 	0, 6, (1 << 0)},	//D61
        {ICON_MODE_DN, 		0, 7, (1 << 1)},	//D72
        {ICON_ACMODE_COOL, 	0, 8, (1 << 0)},	//D79
        {ICON_ACMODE_HEAT, 	0, 6, (1 << 1)},	//D62
        {ICON_ACMODE_AIR, 	0, 6, (1 << 2)},	//D63
        {ICON_ACMODE_DRY, 	0, 7, (1 << 2)},	//D73
        {ICON_SPEED_AUTO, 	0, 7, (1 << 7)},	//D78
        {ICON_SPEED_LOW, 	0, 7, (3 << 5)},	//D77,D76
        {ICON_SPEED_MID, 	0, 7, (3 << 3)},	//D75,D74
        {ICON_SPEED_HIGH, 	0, 7, (1 << 0)},	//D71
        {ICON_BTN_POWER, 	0},
        {ICON_BTN_PAGE, 	0},
        {ICON_BTN_OTHRE, 	0},
    },
    .luminance = 8
};

static Panel panel;
/* private function prototype ----------------------------------------------- */
static void panel_full(void);
static void panel_clear(void);
/* public function prototype ------------------------------------------------ */
void panel_upload(void);
void panel_luminance_set(uint8_t level);
void panel_icon_set(uint8_t id, uint8_t state);
void panel_icon_blink_set(uint8_t id, uint8_t blinkCnt);
void panel_tubex_set(uint8_t tubeId, uint8_t type, uint8_t val, uint8_t state, uint8_t unitState);
void panel_tubex_blink_set(uint8_t tubeId, uint16_t blinkCnt);
void panel_unit_set(uint8_t type);
void panel_unitx_blink_set(uint8_t id, uint16_t blinkCnt);
void panel_process(void);

static void panel_full(void)
{
	uint8_t buf[9] = {0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF};
	tm1640_set_output(buf, 9);

	for(uint8_t i = 0; i < LED_NUM; i++)
    {
        led_set(i, 1);
    }
}
static void panel_clear(void)
{
	memcpy((uint8_t *)&panel, (uint8_t *)&panelDefault, sizeof(Panel));
	panel_upload();
	for(uint8_t i = 0; i < LED_NUM; i++)
    {
        led_set(i, 0);
    }
}
void panel_upload(void)
{
	tm1640_set_output(&panel.code[0], 9);
}
void panel_luminance_set(uint8_t level)
{
	if(panel.luminance != level)
	{
		panel.luminance = level;
		tm1640_set_luminance(panelLuminanceTab[level]);
	}
}

void panel_icon_set(uint8_t id, uint8_t state)
{
    uint8_t updateFlag = 0;
    uint8_t iconshow = 1;
    if(panel.icon[id].state != state)
    {
        panel.icon[id].state = state;
        updateFlag = 1;
    }
    if(panel.icon[id].blink)
    {
        panel.icon[id].blinkTimeout++;
        if(panel.icon[id].blinkTimeout >= 5)
        {
            updateFlag = 1;
        }
    }
    if(updateFlag)
    {
        if(state)
        {
            if(panel.icon[id].blink && (panel.icon[id].blinkTimeout >= 5))
            {
                iconshow = panel.icon[id].blink % 2;
                panel.icon[id].blinkTimeout = 0;
                panel.icon[id].blink--;
            }
            if(iconshow)
            {
                if(id < ICON_BTN_POWER)
                {
                    panel.code[panel.icon[id].codeIndex] |= panel.icon[id].codeBit;
                }
                else
                {
                    led_set((id - ICON_BTN_POWER), 1);
                }
            }
            else
            {
                if(id < ICON_BTN_POWER)
                {
                    panel.code[panel.icon[id].codeIndex] &= ~panel.icon[id].codeBit;
                }
                else
                {
                    led_set((id - ICON_BTN_POWER), 0);
                }
            }
        }
        else
        {
            panel.icon[id].blink = 0;
			if(id < ICON_BTN_POWER)
			{
				panel.code[panel.icon[id].codeIndex] &= ~panel.icon[id].codeBit;
			}
			else
			{
				led_set((id - ICON_BTN_POWER), 0);
			}
        }
    }
}

void panel_icon_blink_set(uint8_t id, uint8_t blinkCnt)
{
	if(blinkCnt > 0)
	{
		panel.icon[id].blink = blinkCnt;
        panel.icon[id].blinkTimeout = 5;
	}
	else
	{
        if(panel.icon[id].blink > 1)
        {
            panel.icon[id].blink = 1;
            panel.icon[id].blinkTimeout = 5;
        }
	}
}
void panel_unit_set(uint8_t type)
{
	if(panel.unit != type)
	{
		panel.unit = type;
	}
}

void panel_tubex_set(uint8_t id, uint8_t type, uint8_t val, uint8_t state, uint8_t unitState)
{
    uint8_t updateFlag = 0;
    uint8_t unitShow = 1;
    if(panel.tube[id].type != type)
    {
        panel.tube[id].type = type;
        updateFlag = 1;
    }
    if(panel.tube[id].val != val)
    {
        panel.tube[id].val = val;
        updateFlag = 1;
    }
    if(panel.tube[id].flag != state)
    {
        panel.tube[id].flag = state;
        updateFlag = 1;
    }
    if(panel.tube[id].unitFlag != unitState)
    {
        panel.tube[id].unitFlag = unitState;
        updateFlag = 1;
    }
    if((panel.tube[id].blink != 0) || (panel.tube[id].unitBlink != 0))
    {
        panel.tube[id].blinkTimeout++;
        if(panel.tube[id].blinkTimeout >= 5)
        {
            updateFlag = 1;
        }
    }
    if(updateFlag)
    {
        if(state)
        {
            switch(type)
            {
                case TUBE_TYPE_DEC:
                {
                    if(val & 0x80)
					{
                        panel.code[panel.tube[id].codeId[0]] = tubeNegative;
                        val &= 0x7F;
					}
					else
					{
						panel.code[panel.tube[id].codeId[0]] = tubeNum[val / 10];
					}
                    panel.code[panel.tube[id].codeId[1]] = tubeNum[val % 10];
                }
                break;
                case TUBE_TYPE_HEX:
				{
					panel.code[panel.tube[id].codeId[0]] = tubeHex[val / 16];
					panel.code[panel.tube[id].codeId[1]] = tubeHex[val % 16];
				}
				break;	
                case TUBE_TYPE_SWITCH:
				{
					if(val)
					{
						panel.code[panel.tube[id].codeId[0]] = tubeAlpha['O' - 'A'];
						panel.code[panel.tube[id].codeId[1]] = tubeAlpha['N' - 'A'];
					}
					else
					{
						panel.code[panel.tube[id].codeId[0]] = tubeAlpha['O' - 'A'];
						panel.code[panel.tube[id].codeId[1]] = tubeAlpha['F' - 'A'];
					}
				}
				break;	
            }
            if(panel.tube[id].blink && (panel.tube[id].blinkTimeout >= 5))
            {
                if(((panel.tube[id].blink) % 2) == 0)
                {
                    panel.code[panel.tube[id].codeId[0]] = 0;
                    panel.code[panel.tube[id].codeId[1]] = 0;
                }
                panel.tube[id].blinkTimeout = 0;
                panel.tube[id].blink--;
            }
        }
        else
        {
            panel.code[panel.tube[id].codeId[0]] = 0;
			panel.code[panel.tube[id].codeId[1]] = 0;
            panel.tube[id].blink = 0;
        }
        if(unitState)
        {
            if(panel.tube[id].unitBlink && (panel.tube[id].blinkTimeout >= 5))
            {
                unitShow = panel.tube[id].unitBlink % 2;
                panel.tube[id].blinkTimeout = 0;
                panel.tube[id].unitBlink--;
            }
            if(panel.unit == UNIT_C)
			{
				if(id < 2)
				{
					panel.code[panel.tube[id].codeId[0]] |= 0x80;
					if(unitShow == 0)
					{
						panel.code[panel.tube[id].codeId[0]] &= ~0x80;
					}
				}
				else
				{
					panel.code[panel.tube[id].codeId[1]] |= 0x80;
					if(unitShow == 0)
					{
						panel.code[panel.tube[id].codeId[1]] &= ~0x80;
					}
				}
			}
			else
			{
				if(id < 2)
				{
					panel.code[panel.tube[id].codeId[1]] |= 0x80;
					if(unitShow == 0)
					{
						panel.code[panel.tube[id].codeId[1]] &= ~0x80;
					}
				}
				else
				{
					panel.code[panel.tube[id].codeId[0]] |= 0x80;
					if(unitShow == 0)
					{
						panel.code[panel.tube[id].codeId[0]] &= ~0x80;
					}
				}
			}
        }
        else
        {
            panel.tube[id].unitBlink = 0;
            panel.code[panel.tube[id].codeId[0]] &= ~0x80;
			panel.code[panel.tube[id].codeId[1]] &= ~0x80;
        }
    }
}

void panel_tubex_blink_set(uint8_t tubeId, uint16_t blinkCnt)
{
	if(blinkCnt > 0)
	{
		panel.tube[tubeId].blink = blinkCnt;
        panel.tube[tubeId].blinkTimeout = 5;
	}
	else
	{
        if(panel.tube[tubeId].blink > 1)
        {
            panel.tube[tubeId].blink = 1;
            panel.tube[tubeId].blinkTimeout = 5;
        }
	}
}
void panel_unitx_blink_set(uint8_t id, uint16_t blinkCnt)
{
	if(blinkCnt > 0)
	{
		panel.tube[id].unitBlink = blinkCnt;
        panel.tube[id].blinkTimeout = 5;
	}
	else
	{
        if(panel.tube[id].unitBlink > 1)
        {
            panel.tube[id].unitBlink = 1;
            panel.tube[id].blinkTimeout = 5;
        }
	}
}
void panel_power_off(void)
{
    /* 空调阀 */
    panel_icon_set(ICON_STATE_AC, 0);
    /* 地暖阀 */
    panel_icon_set(ICON_STATE_DN, 0);
    /* 地暖设定温度 */
    panel_tubex_set(0, TUBE_TYPE_DEC, 0, 0, 0);
    /* 空调设定温度 */
    panel_tubex_set(1, TUBE_TYPE_DEC, 0, 0, 0);
    /* 室温 */
    panel_tubex_set(2, TUBE_TYPE_SWITCH, 0, 1, 0);
    switch(get_system())
    {
        case SYSTEM_AC:
        {
            /* 新风机型指示灯 */
            panel_icon_set(ICON_MODE_FRESH, 0);
            /* 地暖机型指示灯 */
            panel_icon_set(ICON_MODE_DN, 0);
            switch(get_ac_mode())
            {
                case ACMODE_AIR:
                {
                    /* 空调模式 */
                    panel_icon_set(ICON_ACMODE_COOL, 0);
                    panel_icon_set(ICON_ACMODE_HEAT, 0);
                    panel_icon_set(ICON_ACMODE_AIR,  1);
                    panel_icon_set(ICON_ACMODE_DRY,  0);
                }
                break;
                case ACMODE_COOL:
                {
                    /* 空调模式 */
                    panel_icon_set(ICON_ACMODE_COOL, 1);
                    panel_icon_set(ICON_ACMODE_HEAT, 0);
                    panel_icon_set(ICON_ACMODE_AIR,  0);
                    panel_icon_set(ICON_ACMODE_DRY,  0);
                }
                break;
                case ACMODE_HEAT:
                {
                    /* 空调模式 */
                    panel_icon_set(ICON_ACMODE_COOL, 0);
                    panel_icon_set(ICON_ACMODE_HEAT, 1);
                    panel_icon_set(ICON_ACMODE_AIR,  0);
                    panel_icon_set(ICON_ACMODE_DRY,  0);
                }
                break;
                case ACMODE_DRY:
                {
                    /* 空调模式 */
                    panel_icon_set(ICON_ACMODE_COOL, 0);
                    panel_icon_set(ICON_ACMODE_HEAT, 0);
                    panel_icon_set(ICON_ACMODE_AIR,  0);
                    panel_icon_set(ICON_ACMODE_DRY,  1);
                }
                break;
            }
        }
        break;
        case SYSTEM_DN:
        {
            /* 空调模式 */
            panel_icon_set(ICON_ACMODE_COOL, 0);
            panel_icon_set(ICON_ACMODE_HEAT, 0);
            panel_icon_set(ICON_ACMODE_AIR,  0);
            panel_icon_set(ICON_ACMODE_DRY,  0);
            /* 新风机型指示灯 */
            panel_icon_set(ICON_MODE_FRESH, 0);
            /* 地暖机型指示灯 */
            panel_icon_set(ICON_MODE_DN, 1);
        }
        break;
        case SYSTEM_FRESH:
        {
            /* 空调模式 */
            panel_icon_set(ICON_ACMODE_COOL, 0);
            panel_icon_set(ICON_ACMODE_HEAT, 0);
            panel_icon_set(ICON_ACMODE_AIR,  0);
            panel_icon_set(ICON_ACMODE_DRY,  0);
            /* 新风机型指示灯 */
            panel_icon_set(ICON_MODE_FRESH, 1);
            /* 地暖机型指示灯 */
            panel_icon_set(ICON_MODE_DN, 0);
        }
        break;
    }
    /* 风速 */
    panel_icon_set(ICON_SPEED_AUTO, 0);
    panel_icon_set(ICON_SPEED_LOW,  0);
    panel_icon_set(ICON_SPEED_MID,  0);
    panel_icon_set(ICON_SPEED_HIGH,  0);
}
void panel_error(void)
{
    /* 空调阀 */
    panel_icon_set(ICON_STATE_AC, 0);
    /* 地暖阀 */
    panel_icon_set(ICON_STATE_DN, 0);
    /* 地暖设定温度 */
    panel_tubex_set(0, TUBE_TYPE_DEC, 0, 0, 0);
    /* 空调设定温度 */
    panel_tubex_set(1, TUBE_TYPE_DEC, 0, 0, 0);
    /* 室温 */
    panel_tubex_set(2, TUBE_TYPE_HEX, 0xE1, 1, 0);
    switch(get_system())
    {
        case SYSTEM_AC:
        {
            /* 新风机型指示灯 */
            panel_icon_set(ICON_MODE_FRESH, 0);
            /* 地暖机型指示灯 */
            panel_icon_set(ICON_MODE_DN, 0);
            switch(get_ac_mode())
            {
                case ACMODE_AIR:
                {
                    /* 空调模式 */
                    panel_icon_set(ICON_ACMODE_COOL, 0);
                    panel_icon_set(ICON_ACMODE_HEAT, 0);
                    panel_icon_set(ICON_ACMODE_AIR,  1);
                    panel_icon_set(ICON_ACMODE_DRY,  0);
                }
                break;
                case ACMODE_COOL:
                {
                    /* 空调模式 */
                    panel_icon_set(ICON_ACMODE_COOL, 1);
                    panel_icon_set(ICON_ACMODE_HEAT, 0);
                    panel_icon_set(ICON_ACMODE_AIR,  0);
                    panel_icon_set(ICON_ACMODE_DRY,  0);
                }
                break;
                case ACMODE_HEAT:
                {
                    /* 空调模式 */
                    panel_icon_set(ICON_ACMODE_COOL, 0);
                    panel_icon_set(ICON_ACMODE_HEAT, 1);
                    panel_icon_set(ICON_ACMODE_AIR,  0);
                    panel_icon_set(ICON_ACMODE_DRY,  0);
                }
                break;
                case ACMODE_DRY:
                {
                    /* 空调模式 */
                    panel_icon_set(ICON_ACMODE_COOL, 0);
                    panel_icon_set(ICON_ACMODE_HEAT, 0);
                    panel_icon_set(ICON_ACMODE_AIR,  0);
                    panel_icon_set(ICON_ACMODE_DRY,  1);
                }
                break;
            }
        }
        break;
        case SYSTEM_DN:
        {
            /* 空调模式 */
            panel_icon_set(ICON_ACMODE_COOL, 0);
            panel_icon_set(ICON_ACMODE_HEAT, 0);
            panel_icon_set(ICON_ACMODE_AIR,  0);
            panel_icon_set(ICON_ACMODE_DRY,  0);
            /* 新风机型指示灯 */
            panel_icon_set(ICON_MODE_FRESH, 0);
            /* 地暖机型指示灯 */
            panel_icon_set(ICON_MODE_DN, 1);
        }
        break;
        case SYSTEM_FRESH:
        {
            /* 空调模式 */
            panel_icon_set(ICON_ACMODE_COOL, 0);
            panel_icon_set(ICON_ACMODE_HEAT, 0);
            panel_icon_set(ICON_ACMODE_AIR,  0);
            panel_icon_set(ICON_ACMODE_DRY,  0);
            /* 新风机型指示灯 */
            panel_icon_set(ICON_MODE_FRESH, 1);
            /* 地暖机型指示灯 */
            panel_icon_set(ICON_MODE_DN, 0);
        }
        break;
    }
    /* 风速 */
    panel_icon_set(ICON_SPEED_AUTO, 0);
    panel_icon_set(ICON_SPEED_LOW,  0);
    panel_icon_set(ICON_SPEED_MID,  0);
    panel_icon_set(ICON_SPEED_HIGH,  0);
}
/* 显示*/
void panel_process(void)
{
    uint8_t menu;
    switch(get_page())
    {
        case PAGE_SYSTEM:
        {
            /* 联网状态 */
            panel_icon_set(ICON_STATE_WIFI, get_net_state());
            /* 按键锁 */
            panel_icon_set(ICON_STATE_LOCK, get_lock_state());
            /* 节能模式 */
            panel_icon_set(ICON_MODE_ECO, get_eco_state());

            switch(get_system())
            {
                /* 空调模式 */
                case SYSTEM_AC:
                {
                    if(get_power_ac() != 0)
                    {
                        /* 空调阀 */
                        panel_icon_set(ICON_STATE_AC, get_valve_state_ac());
                        /* 地暖阀 */
                        panel_icon_set(ICON_STATE_DN, 0);
                        /* 新风机型指示灯 */
                        panel_icon_set(ICON_MODE_FRESH, 0);
                        /* 地暖机型指示灯 */
                        panel_icon_set(ICON_MODE_DN, 0);
                        /* 地暖 */
                        panel_tubex_set(0, TUBE_TYPE_DEC, 0, 0, 0);
                        /* 室温 */
                        panel_tubex_set(2, TUBE_TYPE_DEC, get_tempSmp(), 1, 1);
                        /* 模式及空调设定温度 */
                        switch(get_ac_mode())
                        {
                            /* 空调通风模式 */
                            case ACMODE_AIR:
                            {
                                /* 空调模式 */
                                panel_icon_set(ICON_ACMODE_COOL, 0);
                                panel_icon_set(ICON_ACMODE_HEAT, 0);
                                panel_icon_set(ICON_ACMODE_AIR,  1);
                                panel_icon_set(ICON_ACMODE_DRY,  0);
                                /* 空调设定温度 */
                                panel_tubex_set(1, TUBE_TYPE_DEC, 0, 0, 0);
                            }
                            break;
                            /* 空调制冷模式 */
                            case ACMODE_COOL:
                            {
                                panel_icon_set(ICON_ACMODE_COOL, 1);
                                panel_icon_set(ICON_ACMODE_HEAT, 0);
                                panel_icon_set(ICON_ACMODE_AIR,  0);
                                panel_icon_set(ICON_ACMODE_DRY,  0);
                                /* 空调设定温度 */
                                panel_tubex_set(1, TUBE_TYPE_DEC, get_tempSet_ac(), 1, 1);
                            }
                            break;
                            /* 空调制热模式 */
                            case ACMODE_HEAT:
                            {
                                panel_icon_set(ICON_ACMODE_COOL, 0);
                                panel_icon_set(ICON_ACMODE_HEAT, 1);
                                panel_icon_set(ICON_ACMODE_AIR,  0);
                                panel_icon_set(ICON_ACMODE_DRY,  0);
                                /* 空调设定温度 */
                                panel_tubex_set(1, TUBE_TYPE_DEC, get_tempSet_ac(), 1, 1);
                            }
                            break;
                            /* 空调除湿模式 */
                            case ACMODE_DRY:
                            {
                                panel_icon_set(ICON_ACMODE_COOL, 0);
                                panel_icon_set(ICON_ACMODE_HEAT, 0);
                                panel_icon_set(ICON_ACMODE_AIR,  0);
                                panel_icon_set(ICON_ACMODE_DRY,  1);
                                /* 空调设定温度 */
                                panel_tubex_set(1, TUBE_TYPE_DEC, 0, 0, 0);
                            }
                            break;
                        }
                        
                        /* 自动风速 */
                        if(get_speedSet_ac() == SPEED_SET_AUTO)
                        {
                            panel_icon_set(ICON_SPEED_AUTO, 1);
                            /* 输出风速 */
                            switch(get_out_speed_ac())
                            {
                                case SPEED_OUT_OFF:
                                {
                                    panel_icon_set(ICON_SPEED_LOW,  0);
                                    panel_icon_set(ICON_SPEED_MID,  0);
                                    panel_icon_set(ICON_SPEED_HIGH, 0);
                                }
                                break;
                                /* 低速 */
                                case SPEED_OUT_LOW:
                                {
                                    panel_icon_set(ICON_SPEED_LOW,  1);
                                    panel_icon_set(ICON_SPEED_MID,  0);
                                    panel_icon_set(ICON_SPEED_HIGH, 0);
                                }
                                break;
                                /* 中速 */
                                case SPEED_OUT_MID:
                                {
                                    panel_icon_set(ICON_SPEED_LOW,  1);
                                    panel_icon_set(ICON_SPEED_MID,  1);
                                    panel_icon_set(ICON_SPEED_HIGH, 0);
                                }
                                break;
                                /* 高速 */
                                case SPEED_OUT_HIGH:
                                {
                                    panel_icon_set(ICON_SPEED_LOW,  1);
                                    panel_icon_set(ICON_SPEED_MID,  1);
                                    panel_icon_set(ICON_SPEED_HIGH, 1);
                                }
                                break;
                            }
                        }
                        else
                        {
                            panel_icon_set(ICON_SPEED_AUTO, 0);
                            
                            switch(get_speedSet_ac())
                            {
                                /* 低速 */
                                case SPEED_SET_LOW:
                                {
                                    panel_icon_set(ICON_SPEED_LOW,  1);
                                    panel_icon_set(ICON_SPEED_MID,  0);
                                    panel_icon_set(ICON_SPEED_HIGH, 0);
                                }
                                break;
                                /* 中速 */
                                case SPEED_SET_MID:
                                {
                                    panel_icon_set(ICON_SPEED_LOW,  1);
                                    panel_icon_set(ICON_SPEED_MID,  1);
                                    panel_icon_set(ICON_SPEED_HIGH, 0);
                                }
                                break;
                                /* 高速 */
                                case SPEED_SET_HIGH:
                                {
                                    panel_icon_set(ICON_SPEED_LOW,  1);
                                    panel_icon_set(ICON_SPEED_MID,  1);
                                    panel_icon_set(ICON_SPEED_HIGH, 1);
                                }
                                break;
                            }
                        }
                    }
                    else
                    {
                        panel_power_off();
                    }
                }
                break;
                /* 地暖模式 */
                case SYSTEM_DN:
                {
                    
                }
                break;
                /* 新风模式 */
                case SYSTEM_FRESH:
                {
                    
                }
                break;
            }            
        }
        break;
        case PAGE_ENGINEER:
        {
            if((get_menu() > MENU_ADDRESS))
            {
                set_menu(MENU_CALIB);
            }
            menu = get_menu();
            /* 空调模式 */
            panel_icon_set(ICON_ACMODE_COOL, 0);
            panel_icon_set(ICON_ACMODE_HEAT, 0);
            panel_icon_set(ICON_ACMODE_AIR,  0);
            panel_icon_set(ICON_ACMODE_DRY,  0);
            /* 风速 */
            panel_icon_set(ICON_SPEED_AUTO, 0);
            panel_icon_set(ICON_SPEED_LOW,  0);
            panel_icon_set(ICON_SPEED_MID,  0);
            panel_icon_set(ICON_SPEED_HIGH,  0);
            /* 菜单项 */
            panel_tubex_set(0, TUBE_TYPE_DEC, menu, 1, 0);
            /* 空调设定温度 */
            panel_tubex_set(1, TUBE_TYPE_DEC, 0, 0, 0);
            /* 配置值 */
            switch(menu)
            {
                case MENU_CALIB:
                {
                    panel_tubex_set(2, TUBE_TYPE_DEC, get_calib_unsigned(), 1, 0);
                }
                break;
                case MENU_ECO:
                {
                    panel_tubex_set(2, TUBE_TYPE_SWITCH, get_eco_state(), 1, 0);
                }
                break;
                case MENU_SPEED_CTRL:
                {
                    panel_tubex_set(2, TUBE_TYPE_SWITCH, get_speed_ctrl_state(), 1, 0);
                }
                break;
                case MENU_LOW_TEMP_PROTECT:
                {
                    panel_tubex_set(2, TUBE_TYPE_SWITCH, get_low_temp_protect_state(), 1, 0);
                }
                break;
                case MENU_MEMORY:
                {
                    panel_tubex_set(2, TUBE_TYPE_SWITCH, get_memory_mode_state(), 1, 0);
                }
                break;
                case MENU_BACKLASH_UP:
                {
                    panel_tubex_set(2, TUBE_TYPE_DEC, get_backlash_up(), 1, 0);
                }
                break;
                case MENU_BACKLASH_DOWN:
                {
                    panel_tubex_set(2, TUBE_TYPE_DEC, get_backlash_down_unsigned(), 1, 0);
                }
                break;
                case MENU_TEMP_SET_LIMIT_UP:
                {
                    panel_tubex_set(2, TUBE_TYPE_DEC, get_tempSet_upLimit(), 1, 0);
                }
                break;
                case MENU_TEMP_SET_LIMIT_DOWN:
                {
                    panel_tubex_set(2, TUBE_TYPE_DEC, get_tempSet_downLimit(), 1, 0);
                }
                break;
                case MENU_COOL_ENABLE:
                {
                    panel_tubex_set(2, TUBE_TYPE_SWITCH, get_ac_cool_enable(), 1, 0);
                }
                break;
                case MENU_HEAT_ENABLE:
                {
                    panel_tubex_set(2, TUBE_TYPE_SWITCH, get_ac_heat_enable(), 1, 0);
                }
                break;
                case MENU_AIR_ENABLE:
                {
                    panel_tubex_set(2, TUBE_TYPE_SWITCH, get_ac_air_enable(), 1, 0);
                }
                break;
                case MENU_DRY_ENABLE:
                {
                    panel_tubex_set(2, TUBE_TYPE_SWITCH, get_ac_dry_enable(), 1, 0);
                }
                break;
                case MENU_UNIT:
                {
                    panel_tubex_set(2, TUBE_TYPE_DEC, 0, 0, 1);
                }
                break;
                case MENU_ADDRESS:
                {
                    panel_tubex_set(2, TUBE_TYPE_HEX, get_device_addr(), 1, 0);
                }
                break;
                default:
                    break;
            }
        }
        break;
        case PAGE_ADVANCE:
        {
            if((get_menu() < MENU_VERSION))
            {
                set_menu(MENU_VERSION);
            }
            menu = get_menu();
            /* 空调模式 */
            panel_icon_set(ICON_ACMODE_COOL, 0);
            panel_icon_set(ICON_ACMODE_HEAT, 0);
            panel_icon_set(ICON_ACMODE_AIR,  0);
            panel_icon_set(ICON_ACMODE_DRY,  0);
            /* 风速 */
            panel_icon_set(ICON_SPEED_AUTO, 0);
            panel_icon_set(ICON_SPEED_LOW,  0);
            panel_icon_set(ICON_SPEED_MID,  0);
            panel_icon_set(ICON_SPEED_HIGH,  0);
            /* 菜单项 */
            panel_tubex_set(0, TUBE_TYPE_DEC, menu, 1, 0);
            /* 空调设定温度 */
            panel_tubex_set(1, TUBE_TYPE_DEC, 0, 0, 0);
            switch(menu)
            {
                case MENU_VERSION:
                {
                    panel_tubex_set(2, TUBE_TYPE_HEX, SOFT_VERSION, 1, 0);
                }
                break;
                case MENU_BAUDRATE:
                {
                    panel_tubex_set(2, TUBE_TYPE_DEC, get_baudrate(), 1, 0);
                }
                break;
                case MENU_MACHINE_ENABLE:
                {
                    panel_tubex_set(2, TUBE_TYPE_DEC, get_machine_enable(), 1, 0);
                }
                break;
                case MENU_LUMINANCE:
                {
                    panel_tubex_set(2, TUBE_TYPE_DEC, get_panel_luminance(), 1, 0);
                }
                break;
                case MENU_RELAY_MODE:
                {
                    panel_tubex_set(2, TUBE_TYPE_DEC, get_relay_mode(), 1, 0);
                }
                break;
            }
        }
        break;
        case PAGE_ERROR:
        {
            panel_error();
        }
        break;
    }
    /* Power背光 */
    panel_icon_set(ICON_BTN_POWER,  get_btn_light_state(BTN_LIGHT_POWER));
    /* Page背光 */
    panel_icon_set(ICON_BTN_PAGE,  get_btn_light_state(BTN_LIGHT_PAGE));
    /* Other背光 */
    panel_icon_set(ICON_BTN_OTHRE,  get_btn_light_state(BTN_LIGHT_OTHER));
    /* 执行显示操作 */
    panel_upload();
}

void panel_init(void)
{
	tm1640_set_luminance(TM1640_CMD_LIGHT_4_16);
	panel_full();
	delay_ms(2000);
	panel_clear();
	delay_ms(1000);
    memcpy(&panel, &panelDefault, sizeof(panelDefault));
}
